﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;

namespace NhConfig.Expression
{
    public class LdPerson<T>
    {
        public string FName { set; get; }

        public void GetPropertyName<K>(Expression<Func<T, K>> propertySelector)
        {
            //var propertyInfo = (fun.Body as MemberExpression).Member as PropertyInfo;
            //if (propertyInfo == null)
            //{
            //    throw new ArgumentException("The lambda expression 'property' should point to a valid Property");
            //}
            //Console.WriteLine("Just a test for it !");
            //return propertyInfo.Name;
            if (propertySelector.Body.NodeType != ExpressionType.MemberAccess)
            {
                throw new ArgumentException(
                    "A property selector expression has an incorrect format");
            }

            MemberExpression memberAccess = propertySelector.Body as MemberExpression;

            if (memberAccess.Member.MemberType != MemberTypes.Property)
            {
                throw new ArgumentException("A selected member is not a property");
            }

            string property = memberAccess.Member.Name;
            Console.WriteLine(property);

        }
    }
}
